package com.sx.pipeline.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * author : zhouyouqiang
 * e-mail : 1638473898@qq.com
 * date   : 2022/5/6 15:41
 * desc   :
 * version: 1.0
 */
public class CompressHelper {
    private String stringName;
    Bitmap inputBitmap = BitmapFactory.decodeFile(stringName);

    /**
     * 采样率压缩
     * @param bitmap
     * @param
     */
    public static File compressQuality(Context context,Bitmap bitmap) {
        File file=new File(context.getExternalCacheDir() + "/" + getRandomFileName() + ".png");
        // 0-100 100为不压缩
        int quality = 30;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 把压缩后的数据存放到baos中
        bitmap.compress(Bitmap.CompressFormat.JPEG,  quality, baos);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 获取采样率
     * @param options
     * @param reqWidth 需要显示/控件定义的宽度
     * @param reqHeight 需要显示/控件定义的高度
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int width = options.outWidth;
        int height = options.outHeight;

        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            int halfWidth = width / 2;
            int halfHeight = height / 2;
            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *=2;
            }
        }

        return inSampleSize;
    }


    public static File saveBitmapFile(Context context,Bitmap bitmap){
        File file=new File(context.getExternalCacheDir() + "/" + getRandomFileName() + ".png");
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 生成随机文件名：当前年月日时分秒+五位随机数
     *
     * @return
     */
    public static String getRandomFileName() {

        SimpleDateFormat simpleDateFormat;

        simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

        Date date = new Date();

        String str = simpleDateFormat.format(date);

        Random random = new Random();

        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数

        return rannum + str;// 当前时间
    }
}
